package cn.chinaiptv.utils;

import java.util.ArrayList;

import cn.chinaiptv.bean.RecordBean;

public class PageUtils {

	public static int getpageCoun(int pagesize) {
		ArrayList<RecordBean> records = RecordUtils.getRecords();
		int pageCount = 0;
		if (records != null || records.size() >= 1)
			pageCount = (records.size() + pagesize - 1) / pagesize;
		return pageCount;
	}

	/**
	 * * 前台分页显示 dspPage()--具体样式再通过CSS控制 形如： 1 2 3 × × × 98 99 100 1 × × × 7 8 9
	 * × × × 100
	 * 
	 * @param url
	 * @param page
	 * @param pagesize
	 * @param rscount
	 * @param pagecount
	 */
	public static String dspPage(String url, int page, int pagesize,
			int rscount, int pagecount) {
		String temppage = "";
		temppage += "<span class=\"pagenum\">";
		if (page > 1) {
			temppage += "<span class=\"page_prev\"><a href=\"" + url + "?page="
					+ (page - 1) + "\">上一页</a></span>";
		} else {
			temppage += "<span class=\"page_prev\"><a href=\"#\">首页</a></span>";
		}
		if (pagecount < 9) {
			for (int p = 1; p <= pagecount; p++) {
				if (p != page)
					temppage += " <span class=\"pager\"><a href=\"" + url
							+ "?page=" + p + "\">" + p + "</a></span>";
				else
					temppage += " <span class=\"pager current\">" + p
							+ "</span>";
			}
		} else {
			if (page <= 3) {
				for (int p = 1; p <= 5; p++) {
					if (p != page)
						temppage += " <span class=\"pager\"><a href=\"" + url
								+ "?page=" + p + "\">" + p + "</a></span>";
					else
						temppage += " <span class=\"pager current\">" + p
								+ "</span>";
				}
				temppage += " <span class=\"pager\">...</span>";
				for (int p = pagecount - 3; p <= pagecount; p++) {
					if (p != page)
						temppage += " <span class=\"pager\"><a href=\"" + url
								+ "?page=" + p + "\">" + p + "</a></span>";
					else
						temppage += " <span class=\"pager current\">" + p
								+ "</span>";
				}
			} else if (pagecount - page <= 3) {
				for (int p = 1; p <= 3; p++) {
					temppage += " <span class=\"pager\"><a href=\"" + url
							+ "?page=" + p + "\">" + p + "</a></span>";
				}
				temppage += "<span class=\"pager\">...</span>";
				for (int p = pagecount - 4; p <= pagecount; p++) {
					if (p != page) {
						temppage += " <span class=\"pager\"><a href=\"" + url
								+ "?page=" + p + "\">" + p + "</a></span>";
					} else {
						temppage += " <span class=\"pager current\">" + p
								+ "</span>";
					}
				}
			} else {
				temppage += " <span class=\"pager\"><a href=\"" + url
						+ "?page=1\">1</a></span>";
				temppage += " <span class=\"pager\">...</span>";
				for (int p = page - 2; p <= page + 2; p++) {
					if (p != page) {
						temppage += " <span class=\"pager\"><a href=\"" + url
								+ "?page=" + p + "\">" + p + "</a></span>";
					} else {
						temppage += " <span class=\"pager current\">" + p
								+ "</span>";
					}
				}
				temppage += "<span class=\"pager\">...</span>";
				temppage += " <span class=\"pager\"><a href=\"" + url
						+ "?page=" + pagecount + "\">" + pagecount
						+ "</a></span>";
			}
		}

		if (page <= pagecount - 1) {
			temppage += "<span class=\"page_prev\"><a href=\"" + url + "?page="
					+ (page + 1) + "\">下一页</a></span>";
		} else {
			temppage += "<span class=\"page_prev\"><a href=\"#\">尾页</a></span>";
		}
		temppage += "</span>";

		return temppage;
	}
}
